package com.boot.web.serviceImpl;

import com.boot.web.dao.*;
import com.boot.web.entity.*;
import com.boot.web.vo.OrdersVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boot.web.service.OrdersService;
import com.boot.base.serviceImpl.BaseServiceImpl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单业务层接口实现
 * @author 【zhouJie】
 * @date 2021-06-05 14:45:19
 */
@Service
public class OrdersServiceImpl extends BaseServiceImpl<Orders> implements OrdersService {

    private static final Logger logger = LoggerFactory.getLogger(OrdersServiceImpl.class);

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private AccountRecordDao accountRecordDao;

    @Autowired
    private TodayCollageProfitDao todayCollageProfitDao;

    @Override
    public List<OrdersVO> getOrdersVOPageList(Map<String, Object> map) {
        return ordersDao.queryOrdersVOPageList(map);
    }

    /**
     * 添加拼团分润记录
     * @param agentCode
     * @param parentUserId
     * @param collageBalance
     * @param d
     * @param orderNo
     */
    public void shareProfitRecord(String agentCode, Integer parentUserId, BigDecimal collageBalance, BigDecimal d,  String orderNo){
        //Account parentAcc = accountDao.queryAccountByUserId(parentUserId);
        BigDecimal profit = collageBalance.multiply(d).setScale(4, BigDecimal.ROUND_DOWN);
        //parentAcc.setCollageBalance(parentAcc.getCollageBalance().add(profit));
        //accountDao.update(parentAcc);
        AccountRecord ar = new AccountRecord();
        ar.setIntoUserId(parentUserId);
        ar.setOrderNo(orderNo);
        ar.setType("8");
        ar.setSign("1");
        ar.setUpdateTime(new Date());
        ar.setAmount(profit);
        ar.setInfo(agentCode + "参团成功! 您获得拼团红包：" + profit);
        accountRecordDao.save(ar);
        // 佣金累计起来
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String time = df.format(new Date());
        TodayCollageProfit tcp = todayCollageProfitDao.queryByUserIdAndToday(parentUserId, time); // 获取今日的利润分配信息
        if(tcp == null){
            // 创建今日tcf
            TodayCollageProfit newTcp = new TodayCollageProfit();
            newTcp.setAmount(profit);
            newTcp.setStatus("0");
            newTcp.setUpdateTime(new Date());
            newTcp.setUserId(parentUserId);
            todayCollageProfitDao.save(newTcp);
        }else{
            tcp.setAmount(tcp.getAmount().add(profit));
            todayCollageProfitDao.update(tcp);
        }
    }

    /**
     * 分润
     * @param userId
     * @param voucher
     * @param orderNo
     */
    @Override
    public void handleCollageProfit30(Integer userId, BigDecimal voucher, String orderNo) {

        BigDecimal bd5 = new BigDecimal(0.05); // 幼蜂 5% -- 2
        BigDecimal bd10 = new BigDecimal(0.1); // 小蜜蜂 10% -- 3
        BigDecimal bd13 = new BigDecimal(0.13); // 黄蜂 13% -- 4
        BigDecimal bd16 = new BigDecimal(0.16); // 蜂王 16% -- 5
        BigDecimal bd20 = new BigDecimal(0.2); // 蜂皇 20% -- 6
        BigDecimal bd7 = new BigDecimal(0.07); // 就近蜂皇 7%

        Customer cu = customerDao.queryById(userId);
        if(cu.getParentId() != null && cu.getParentId() != 1198 && cu.getLevelId() != 7) {
            Customer pCu = customerDao.queryById(cu.getParentId());
            if (pCu != null) {
                //if("1".equals(pCu.getIsActive())){
                    if(pCu.getLevelId() == 1){
                        // 分团队
                        // 找一个等级大于2的人，2没有团队利润，需要再招人
                        Customer newCu2 = queryCustomer(pCu, 2);
                        if(newCu2 != null){
                            //if("1".equals(newCu2.getIsActive())){
                                if(newCu2.getLevelId() == 3){
                                    shareProfitRecord(cu.getAgentCode(), newCu2.getId(), voucher, bd10, orderNo);

                                    // 找一个等级大于3的人
                                    Customer newCu3 = queryCustomer(newCu2, 3);
                                    if(newCu3 != null){
                                        //if("1".equals(newCu3.getIsActive())){
                                            if(newCu3.getLevelId() == 4){
                                                shareProfitRecord(cu.getAgentCode(), newCu3.getId(), voucher, bd13.subtract(bd10), orderNo);

                                                // 找一个等级大于4的人
                                                Customer newCu4 = queryCustomer(newCu3, 4);
                                                if(newCu4 != null){
                                                    //if("1".equals(newCu4.getIsActive())){
                                                        if(newCu4.getLevelId() == 5){
                                                            shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd16.subtract(bd13), orderNo);

                                                            // 找一个等级大于5的人
                                                            Customer newCu5 = queryCustomer(newCu4, 5);
                                                            if(newCu5 != null){
                                                                //if("1".equals(newCu5.getIsActive())){
                                                                    if(newCu5.getLevelId() == 6){
                                                                        shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                                        // 分bd7
                                                                        Customer newCu6 = queryCustomer(newCu5, 5);
                                                                        if(newCu6 != null){
                                                                            //if("1".equals(newCu6.getIsActive())){
                                                                                if(newCu6.getLevelId() == 6){
                                                                                    shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                                                }
                                                                            //}
                                                                        }

                                                                    }
                                                                //}
                                                            }

                                                        }else if(newCu4.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd20.subtract(bd10), orderNo);

                                                            // 分bd7
                                                            Customer newCu6 = queryCustomer(newCu4, 5);
                                                            if(newCu6 != null){
                                                                //if("1".equals(newCu6.getIsActive())){
                                                                    if(newCu6.getLevelId() == 6){
                                                                        shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd10)).multiply(bd7), orderNo);
                                                                    }
                                                                //}
                                                            }

                                                        }
                                                    //}
                                                }

                                            }else if(newCu3.getLevelId() == 5){
                                                shareProfitRecord(cu.getAgentCode(), newCu3.getId(), voucher, bd16.subtract(bd10), orderNo);

                                                // 找一个等级大于5的人
                                                Customer newCu5 = queryCustomer(newCu3, 5);
                                                if(newCu5 != null){
                                                    //if("1".equals(newCu5.getIsActive())){
                                                        if(newCu5.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                            // 分bd7
                                                            Customer newCu6 = queryCustomer(newCu5, 5);
                                                            if(newCu6 != null){
                                                                //if("1".equals(newCu6.getIsActive())){
                                                                    if(newCu6.getLevelId() == 6){
                                                                        shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                                    }
                                                                //}
                                                            }

                                                        }
                                                    //}
                                                }

                                            }else if(newCu3.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu3.getId(), voucher, bd20.subtract(bd10), orderNo);

                                                // 分bd7
                                                Customer newCu6 = queryCustomer(newCu3, 5);
                                                if(newCu6 != null){
                                                    //if("1".equals(newCu6.getIsActive())){
                                                        if(newCu6.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd10)).multiply(bd7), orderNo);
                                                        }
                                                    //}
                                                }

                                            }
                                        //}
                                    }

                                }else if(newCu2.getLevelId() == 4){
                                    shareProfitRecord(cu.getAgentCode(), newCu2.getId(), voucher, bd13.subtract(bd5), orderNo);

                                    // 找一个等级大于4的人
                                    Customer newCu4 = queryCustomer(newCu2, 4);
                                    if(newCu4 != null){
                                        //if("1".equals(newCu4.getIsActive())){
                                            if(newCu4.getLevelId() == 5){
                                                shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd16.subtract(bd13), orderNo);

                                                // 找一个等级大于5的人
                                                Customer newCu5 = queryCustomer(newCu4, 5);
                                                if(newCu5 != null){
                                                    //if("1".equals(newCu5.getIsActive())){
                                                        if(newCu5.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                            // 分bd7
                                                            Customer newCu6 = queryCustomer(newCu5, 5);
                                                            if(newCu6 != null){
                                                                //if("1".equals(newCu6.getIsActive())){
                                                                    if(newCu6.getLevelId() == 6){
                                                                        shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                                    }
                                                                //}
                                                            }

                                                        }
                                                    //}
                                                }

                                            }else if(newCu4.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd20.subtract(bd10), orderNo);

                                                // 分bd7
                                                Customer newCu6 = queryCustomer(newCu4, 5);
                                                if(newCu6 != null){
                                                    //if("1".equals(newCu6.getIsActive())){
                                                        if(newCu6.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd10)).multiply(bd7), orderNo);
                                                        }
                                                    //}
                                                }

                                            }
                                        //}
                                    }

                                }else if(newCu2.getLevelId() == 5){
                                    shareProfitRecord(cu.getAgentCode(), newCu2.getId(), voucher, bd16.subtract(bd5), orderNo);

                                    // 找一个等级大于5的人
                                    Customer newCu5 = queryCustomer(newCu2, 5);
                                    if(newCu5 != null){
                                        //if("1".equals(newCu5.getIsActive())){
                                            if(newCu5.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                // 分bd7
                                                Customer newCu6 = queryCustomer(newCu5, 5);
                                                if(newCu6 != null){
                                                    //if("1".equals(newCu6.getIsActive())){
                                                        if(newCu6.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                        }
                                                    //}
                                                }

                                            }
                                        //}
                                    }

                                }else if(newCu2.getLevelId() == 6){
                                    shareProfitRecord(cu.getAgentCode(), newCu2.getId(), voucher, bd20.subtract(bd5), orderNo);

                                    // 分bd7
                                    Customer newCu6 = queryCustomer(newCu2, 5);
                                    if(newCu6 != null){
                                        //if("1".equals(newCu6.getIsActive())){
                                            if(newCu6.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd5)).multiply(bd7), orderNo);
                                            }
                                        //}
                                    }

                                }
                            //}
                        }
                    }
                    // 分一代
                    if(pCu.getLevelId() == 2){
                        shareProfitRecord(cu.getAgentCode(), pCu.getId(), voucher, bd5, orderNo);

                        // 分团队
                        // 找一个等级大于2的人，2没有团队利润，需要再招人
                        Customer newCu2 = queryCustomer(pCu, 2);
                        if(newCu2 != null){
                            //if("1".equals(newCu2.getIsActive())){
                                if(newCu2.getLevelId() == 3){
                                    shareProfitRecord(cu.getAgentCode(), newCu2.getId(), voucher, bd10.subtract(bd5), orderNo);

                                    // 找一个等级大于3的人
                                    Customer newCu3 = queryCustomer(newCu2, 3);
                                    if(newCu3 != null){
                                        //if("1".equals(newCu3.getIsActive())){
                                            if(newCu3.getLevelId() == 4){
                                                shareProfitRecord(cu.getAgentCode(), newCu3.getId(), voucher, bd13.subtract(bd10), orderNo);

                                                // 找一个等级大于4的人
                                                Customer newCu4 = queryCustomer(newCu3, 4);
                                                if(newCu4 != null){
                                                    //if("1".equals(newCu4.getIsActive())){
                                                        if(newCu4.getLevelId() == 5){
                                                            shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd16.subtract(bd13), orderNo);

                                                            // 找一个等级大于5的人
                                                            Customer newCu5 = queryCustomer(newCu4, 5);
                                                            if(newCu5 != null){
                                                                //if("1".equals(newCu5.getIsActive())){
                                                                    if(newCu5.getLevelId() == 6){
                                                                        shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                                        // 分bd7
                                                                        Customer newCu6 = queryCustomer(newCu5, 5);
                                                                        if(newCu6 != null){
                                                                            //if("1".equals(newCu6.getIsActive())){
                                                                                if(newCu6.getLevelId() == 6){
                                                                                    shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                                                }
                                                                            //}
                                                                        }

                                                                    }
                                                                //}
                                                            }

                                                        }else if(newCu4.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd20.subtract(bd10), orderNo);

                                                            // 分bd7
                                                            Customer newCu6 = queryCustomer(newCu4, 5);
                                                            if(newCu6 != null){
                                                                //if("1".equals(newCu6.getIsActive())){
                                                                    if(newCu6.getLevelId() == 6){
                                                                        shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd10)).multiply(bd7), orderNo);
                                                                    }
                                                                //}
                                                            }

                                                        }
                                                    //}
                                                }

                                            }else if(newCu3.getLevelId() == 5){
                                                shareProfitRecord(cu.getAgentCode(), newCu3.getId(), voucher, bd16.subtract(bd10), orderNo);

                                                // 找一个等级大于5的人
                                                Customer newCu5 = queryCustomer(newCu3, 5);
                                                if(newCu5 != null){
                                                    //if("1".equals(newCu5.getIsActive())){
                                                        if(newCu5.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                            // 分bd7
                                                            Customer newCu6 = queryCustomer(newCu5, 5);
                                                            if(newCu6 != null){
                                                                //if("1".equals(newCu6.getIsActive())){
                                                                    if(newCu6.getLevelId() == 6){
                                                                        shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                                    }
                                                                //}
                                                            }

                                                        }
                                                    //}
                                                }

                                            }else if(newCu3.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu3.getId(), voucher, bd20.subtract(bd10), orderNo);

                                                // 分bd7
                                                Customer newCu6 = queryCustomer(newCu3, 5);
                                                if(newCu6 != null){
                                                    //if("1".equals(newCu6.getIsActive())){
                                                        if(newCu6.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd10)).multiply(bd7), orderNo);
                                                        }
                                                    //}
                                                }

                                            }
                                        //}
                                    }

                                }else if(newCu2.getLevelId() == 4){
                                    shareProfitRecord(cu.getAgentCode(), newCu2.getId(), voucher, bd13.subtract(bd5), orderNo);

                                    // 找一个等级大于4的人
                                    Customer newCu4 = queryCustomer(newCu2, 4);
                                    if(newCu4 != null){
                                        //if("1".equals(newCu4.getIsActive())){
                                            if(newCu4.getLevelId() == 5){
                                                shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd16.subtract(bd13), orderNo);

                                                // 找一个等级大于5的人
                                                Customer newCu5 = queryCustomer(newCu4, 5);
                                                if(newCu5 != null){
                                                    //if("1".equals(newCu5.getIsActive())){
                                                        if(newCu5.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                            // 分bd7
                                                            Customer newCu6 = queryCustomer(newCu5, 5);
                                                            if(newCu6 != null){
                                                                //if("1".equals(newCu6.getIsActive())){
                                                                    if(newCu6.getLevelId() == 6){
                                                                        shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                                    }
                                                                //}
                                                            }

                                                        }
                                                    //}
                                                }

                                            }else if(newCu4.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd20.subtract(bd10), orderNo);

                                                // 分bd7
                                                Customer newCu6 = queryCustomer(newCu4, 5);
                                                if(newCu6 != null){
                                                    //if("1".equals(newCu6.getIsActive())){
                                                        if(newCu6.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd10)).multiply(bd7), orderNo);
                                                        }
                                                    //}
                                                }

                                            }
                                        //}
                                    }

                                }else if(newCu2.getLevelId() == 5){
                                    shareProfitRecord(cu.getAgentCode(), newCu2.getId(), voucher, bd16.subtract(bd5), orderNo);

                                    // 找一个等级大于5的人
                                    Customer newCu5 = queryCustomer(newCu2, 5);
                                    if(newCu5 != null){
                                        //if("1".equals(newCu5.getIsActive())){
                                            if(newCu5.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                // 分bd7
                                                Customer newCu6 = queryCustomer(newCu5, 5);
                                                if(newCu6 != null){
                                                    //if("1".equals(newCu6.getIsActive())){
                                                        if(newCu6.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                        }
                                                    //}
                                                }

                                            }
                                        //}
                                    }

                                }else if(newCu2.getLevelId() == 6){
                                    shareProfitRecord(cu.getAgentCode(), newCu2.getId(), voucher, bd20.subtract(bd5), orderNo);

                                    // 分bd7
                                    Customer newCu6 = queryCustomer(newCu2, 5);
                                    if(newCu6 != null){
                                        //if("1".equals(newCu6.getIsActive())){
                                            if(newCu6.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd5)).multiply(bd7), orderNo);
                                            }
                                        //}
                                    }

                                }
                            //}
                        }

                    }else if(pCu.getLevelId() == 3){
                        shareProfitRecord(cu.getAgentCode(), pCu.getId(), voucher, bd10, orderNo);

                        // 分团队
                        // 找一个等级大于3的人
                        Customer newCu3 = queryCustomer(pCu, 3);
                        if(newCu3 != null){
                            //if("1".equals(newCu3.getIsActive())){
                                if(newCu3.getLevelId() == 4){
                                    shareProfitRecord(cu.getAgentCode(), newCu3.getId(), voucher, bd13.subtract(bd10), orderNo);

                                    // 找一个等级大于4的人
                                    Customer newCu4 = queryCustomer(newCu3, 4);
                                    if(newCu4 != null){
                                        //if("1".equals(newCu4.getIsActive())){
                                            if(newCu4.getLevelId() == 5){
                                                shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd16.subtract(bd13), orderNo);

                                                // 找一个等级大于5的人
                                                Customer newCu5 = queryCustomer(newCu4, 5);
                                                if(newCu5 != null){
                                                    //if("1".equals(newCu5.getIsActive())){
                                                        if(newCu5.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                            // 分bd7
                                                            Customer newCu6 = queryCustomer(newCu5, 5);
                                                            if(newCu6 != null){
                                                                //if("1".equals(newCu6.getIsActive())){
                                                                    if(newCu6.getLevelId() == 6){
                                                                        shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                                    }
                                                                //}
                                                            }

                                                        }
                                                    //}
                                                }

                                            }else if(newCu4.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd20.subtract(bd10), orderNo);

                                                // 分bd7
                                                Customer newCu6 = queryCustomer(newCu4, 5);
                                                if(newCu6 != null){
                                                    //if("1".equals(newCu6.getIsActive())){
                                                        if(newCu6.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd10)).multiply(bd7), orderNo);
                                                        }
                                                    //}
                                                }

                                            }
                                        //}
                                    }

                                }else if(newCu3.getLevelId() == 5){
                                    shareProfitRecord(cu.getAgentCode(), newCu3.getId(), voucher, bd16.subtract(bd10), orderNo);

                                    // 找一个等级大于5的人
                                    Customer newCu5 = queryCustomer(newCu3, 5);
                                    if(newCu5 != null){
                                        //if("1".equals(newCu5.getIsActive())){
                                            if(newCu5.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                // 分bd7
                                                Customer newCu6 = queryCustomer(newCu5, 5);
                                                if(newCu6 != null){
                                                    //if("1".equals(newCu6.getIsActive())){
                                                        if(newCu6.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                        }
                                                    //}
                                                }

                                            }
                                        //}
                                    }

                                }else if(newCu3.getLevelId() == 6){
                                    shareProfitRecord(cu.getAgentCode(), newCu3.getId(), voucher, bd20.subtract(bd10), orderNo);

                                    // 分bd7
                                    Customer newCu6 = queryCustomer(newCu3, 5);
                                    if(newCu6 != null){
                                        //if("1".equals(newCu6.getIsActive())){
                                            if(newCu6.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd10)).multiply(bd7), orderNo);
                                            }
                                        //}
                                    }

                                }
                            //}
                        }

                    }else if(pCu.getLevelId() == 4){
                        shareProfitRecord(cu.getAgentCode(), pCu.getId(), voucher, bd13, orderNo);

                        // 分团队
                        // 找一个等级大于4的人
                        Customer newCu4 = queryCustomer(pCu, 4);
                        if(newCu4 != null){
                            //if("1".equals(newCu4.getIsActive())){
                                if(newCu4.getLevelId() == 5){
                                    shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd16.subtract(bd13), orderNo);

                                    // 找一个等级大于5的人
                                    Customer newCu5 = queryCustomer(newCu4, 5);
                                    if(newCu5 != null){
                                        //if("1".equals(newCu5.getIsActive())){
                                            if(newCu5.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                                // 分bd7
                                                Customer newCu6 = queryCustomer(newCu5, 5);
                                                if(newCu6 != null){
                                                    //if("1".equals(newCu6.getIsActive())){
                                                        if(newCu6.getLevelId() == 6){
                                                            shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                                        }
                                                    //}
                                                }

                                            }
                                        //}
                                    }

                                }else if(newCu4.getLevelId() == 6){
                                    shareProfitRecord(cu.getAgentCode(), newCu4.getId(), voucher, bd20.subtract(bd10), orderNo);

                                    // 分bd7
                                    Customer newCu6 = queryCustomer(newCu4, 5);
                                    if(newCu6 != null){
                                        //if("1".equals(newCu6.getIsActive())){
                                            if(newCu6.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd10)).multiply(bd7), orderNo);
                                            }
                                        //}
                                    }

                                }
                            //}
                        }

                    }else if(pCu.getLevelId() == 5){
                        shareProfitRecord(cu.getAgentCode(), pCu.getId(), voucher, bd16, orderNo);

                        // 分团队
                        // 找一个等级大于5的人
                        Customer newCu5 = queryCustomer(pCu, 5);
                        if(newCu5 != null){
                            //if("1".equals(newCu5.getIsActive())){
                                if(newCu5.getLevelId() == 6){
                                    shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.subtract(bd16), orderNo);

                                    // 分bd7
                                    Customer newCu6 = queryCustomer(newCu5, 5);
                                    if(newCu6 != null){
                                        //if("1".equals(newCu6.getIsActive())){
                                            if(newCu6.getLevelId() == 6){
                                                shareProfitRecord(cu.getAgentCode(), newCu6.getId(), voucher, (bd20.subtract(bd16)).multiply(bd7), orderNo);
                                            }
                                        //}
                                    }

                                }
                            //}
                        }

                    }else if(pCu.getLevelId() == 6){
                        shareProfitRecord(cu.getAgentCode(), pCu.getId(), voucher, bd20, orderNo);

                        // 分团队
                        // 找一个等级大于5的人
                        Customer newCu5 = queryCustomer(pCu, 5);
                        if(newCu5 != null){
                            //if("1".equals(newCu5.getIsActive())){
                                if(newCu5.getLevelId() == 6){
                                    shareProfitRecord(cu.getAgentCode(), newCu5.getId(), voucher, bd20.multiply(bd7), orderNo);
                                }
                            //}
                        }

                    }
                //}
            }
        }
    }

    /**
     * 获取就近等级高于 levelId 的人
     * @param customer
     * @param levelId
     * @return
     */
    public Customer queryCustomer(Customer customer, Integer levelId){
        Integer newId = 0;
        if (customer.getParentId() != null && customer.getParentId() != 1198 && customer.getLevelId() != 7) {
            newId = customer.getParentId();
        }
        Customer newCustomer = customerDao.queryById(newId);
        if(newCustomer == null){
            return newCustomer;
        }
        if (newCustomer.getLevelId() > levelId) {
            return newCustomer;
        } else {
            while (newCustomer.getLevelId() <= levelId) {
                newCustomer = customerDao.queryById(newId);
                if(newCustomer == null){
                    break;
                }
                if (newCustomer.getLevelId() > levelId) {
                    break;
                } else {
                    if (newCustomer.getParentId() != null && newCustomer.getParentId() != 1198 && newCustomer.getLevelId() != 7) {
                        newId = newCustomer.getParentId();
                    }else{
                        break;
                    }
                }
            }
        }
        return newCustomer;
    }

}
